package com.niuquan.activitie;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.IYWPushListener;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.YWLoginParam;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.alibaba.mobileim.contact.IYWContact;
import com.alibaba.mobileim.conversation.IYWConversationService;
import com.alibaba.mobileim.conversation.YWMessage;
import com.alibaba.mobileim.gingko.model.tribe.YWTribe;
import com.niuquan.base_data.Messages;
import com.niuquan.base_function.NetTools;
import com.niuquan.base_function.cache_core;
import com.niuquan.modal.ChatListItems;
import com.niuquan.modal.ContactsItem;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
 */
public class ExpertCircle extends Application {
    public YWIMKit mIMKit;
    cache_core core;
    //recent contacts
    Messages m;
    public static ArrayList<ChatListItems> recentContacts = new ArrayList<ChatListItems>();
    //all contacts
    public static ArrayList<ContactsItem> Contactsall = new ArrayList<ContactsItem>();
    @Override
    public void onCreate() {
        final String APP_KEY = "23173937";
//第一个参数是Application Context
//这里的APP_KEY即应用创建时申请的APP_KEY
        YWAPI.init(this, APP_KEY);
        mIMKit = YWAPI.getIMKitInstance();
        core = cache_core.getInstance(this.getApplicationContext());
        Toast.makeText(ExpertCircle.this, "初始化成功", Toast.LENGTH_SHORT)
                .show();


        String userId = "18328588447";
        String password = "e10adc3949ba59abbe56e057f20f883e";
        IYWLoginService loginService = mIMKit.getLoginService();
        YWLoginParam loginParam = YWLoginParam.createLoginParam(userId, password);
        loginService.login(loginParam, new IWxCallback() {

            @Override
            public void onSuccess(Object... arg0) {
            }

            @Override
            public void onProgress(int arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onError(int errCode, String description) {
                //如果登录失败，errCode为错误码,description是错误的具体描述信息
            }
        });



        mIMKit.setEnableNotification(true);//开启通知栏提示
        mIMKit.setAppName("我的OpenIM");//通知栏显示的名称
        mIMKit.setResId(R.drawable.aliwx_notification_bg);//开发者可以换成自定义的Icon
        IYWPushListener msgPushListener = new IYWPushListener() {
            @Override
            public void onPushMessage(IYWContact iywContact, YWMessage ywMessage) {

            }
            @Override
            public void onPushMessage(YWTribe ywTribe, YWMessage ywMessage) {
                Log.d("hehehe",ywMessage.getAuthorUserId());
                Log.d("hehehe",ywMessage.getMessageBody().getContent());
            }
        };
            IYWConversationService conversationService = mIMKit.getConversationService();
//如果之前add过，请清除
            conversationService.removePushListener(msgPushListener);
//增加新消息到达的通知
            conversationService.addPushListener(msgPushListener);
        ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
        list.add(new BasicNameValuePair("account", "18328588447"));
        list.add(new BasicNameValuePair("password", "123456"));
        m = new Messages(list);
        new Thread() {
            @Override
            public void run() {
                String s =NetTools.send("http://172.27.35.1:8080/expertcircle/userLogin.action",m);
                System.out.println(s);
                try {
                    JSONObject j = new JSONObject(s);
                    core.setUserInfo("id",j.getString("id"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                super.run();
            }
        }.start();
        super.onCreate();
    }
}